import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yi_shop/route/index.dart';
import 'package:yi_shop/utils/colors.dart';
import 'package:yi_shop/utils/styles.dart';
import 'package:yi_shop/utils/svg.dart';

class UnusedDiscountController extends GetxController {}

class UnusedDiscountView extends GetView<UnusedDiscountController> {
  const UnusedDiscountView({super.key});

  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      slivers: [
        SliverList.builder(
          itemBuilder: (c, index) {
            return InkWell(
              onTap: () {
                Get.toNamed(RoutePath.couponInfo);
              },
              child: Container(
                margin: const EdgeInsets.only(top: 15, left: 10, right: 10),
                height: 100,
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Material(
                      color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF048),
                      shape: _CouponShapeBorder(),
                      child: Container(
                        constraints: const BoxConstraints.expand(width: 103, height: 100),
                        child: Column(
                          children: [
                            Container(
                              padding: EdgeInsets.only(top: 15.h, bottom: 15.h),
                              alignment: Alignment.center,
                              child: Text(
                                '￥20',
                                style: StylesUtils.customTextStyle(
                                    fontWeight: FontWeight.w700, fontSize: 28.sp),
                              ),
                            ),
                            Container(
                              alignment: Alignment.center,
                              child: Text(
                                '满200元可用',
                                style: StylesUtils.customTextStyle(
                                    fontSize: StylesUtils.fontSize13, color: ColorsUtils.colorF2f2),
                              ),
                            )
                          ],
                        ),
                      ),
                    ),
                    Expanded(
                        child: Container(
                      padding: EdgeInsets.only(left: 10.w),
                      decoration: BoxDecoration(
                          color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorFfff)),
                      child: Stack(
                        children: [
                          Column(
                            children: [
                              Container(
                                padding: EdgeInsets.only(top: 15.h, bottom: 25.h),
                                alignment: Alignment.centerLeft,
                                child: Text(
                                  '仅限购买服饰类部分商品',
                                  style: StylesUtils.customTextStyle(color: ColorsUtils.color6666),
                                ),
                              ),
                              Container(
                                alignment: Alignment.centerLeft,
                                child: Text(
                                  '2018-11-03至2018-11-20',
                                  style: StylesUtils.customTextStyle(
                                      fontSize: StylesUtils.fontSize13,
                                      color: ColorsUtils.color9999),
                                ),
                              )
                            ],
                          ),
                          Positioned(
                            top: 0,
                            right: 0,
                            width: 60,
                            height: 60,
                            child: ClipPath(
                              clipper: _MyCustomClipper(),
                              child: Container(
                                  alignment: Alignment.center,
                                  constraints: const BoxConstraints.expand(width: 60, height: 60),
                                  decoration: BoxDecoration(
                                      color: ColorsUtils.parseColorFromHexString(
                                          ColorsUtils.colorF26f)),
                                  child: Transform.rotate(
                                    // 45度
                                    angle: pi / 4,
                                    child: Container(
                                      alignment: Alignment.topCenter,
                                      constraints:
                                          const BoxConstraints.expand(width: 60, height: 35),
                                      child: Text(
                                        '快过期',
                                        style: StylesUtils.customTextStyle(
                                            fontSize: StylesUtils.fontSize13),
                                      ),
                                    ),
                                  )),
                            ),
                          )
                        ],
                      ),
                    ))
                  ],
                ),
              ),
            );
          },
          itemCount: 10,
        )
      ],
    );
  }
}

class UsedDiscountController extends GetxController {}

class UsedDiscountView extends GetView<UnusedDiscountController> {
  const UsedDiscountView({super.key});

  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      slivers: [
        SliverList.builder(
          itemBuilder: (c, index) {
            return Container(
              margin: const EdgeInsets.only(top: 15, left: 10, right: 10),
              height: 100,
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Material(
                    color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF048),
                    shape: _CouponShapeBorder(),
                    child: Container(
                      constraints: const BoxConstraints.expand(width: 103, height: 100),
                      child: Column(
                        children: [
                          Container(
                            padding: EdgeInsets.only(top: 15.h, bottom: 15.h),
                            alignment: Alignment.center,
                            child: Text(
                              '￥20',
                              style: StylesUtils.customTextStyle(
                                  fontWeight: FontWeight.w700, fontSize: 28.sp),
                            ),
                          ),
                          Container(
                            alignment: Alignment.center,
                            child: Text(
                              '满200元可用',
                              style: StylesUtils.customTextStyle(
                                  fontSize: StylesUtils.fontSize13, color: ColorsUtils.colorF2f2),
                            ),
                          )
                        ],
                      ),
                    ),
                  ),
                  Expanded(
                      child: Container(
                    padding: EdgeInsets.only(left: 10.w),
                    decoration: BoxDecoration(
                        color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorFfff)),
                    child: Stack(
                      children: [
                        Column(
                          children: [
                            Container(
                              padding: EdgeInsets.only(top: 15.h, bottom: 25.h),
                              alignment: Alignment.centerLeft,
                              child: Text(
                                '仅限购买服饰类部分商品',
                                style: StylesUtils.customTextStyle(color: ColorsUtils.color6666),
                              ),
                            ),
                            Container(
                              alignment: Alignment.centerLeft,
                              child: Text(
                                '2018-11-03至2018-11-20',
                                style: StylesUtils.customTextStyle(
                                    fontSize: StylesUtils.fontSize13, color: ColorsUtils.color9999),
                              ),
                            )
                          ],
                        ),
                        Positioned(
                          right: 10,
                          bottom: 0,
                          top: 0,
                          child: Center(
                            child: SvgUtils.assetName('u15530', width: 48, height: 48),
                          ),
                        )
                      ],
                    ),
                  ))
                ],
              ),
            );
          },
          itemCount: 10,
        )
      ],
    );
  }
}

class ExpiredDiscountController extends GetxController {}

class ExpiredDiscountView extends GetView<ExpiredDiscountController> {
  const ExpiredDiscountView({super.key});

  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      slivers: [
        SliverList.builder(
          itemBuilder: (c, index) {
            return Container(
              margin: const EdgeInsets.only(top: 15, left: 10, right: 10),
              height: 100,
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Material(
                    color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorD7d7),
                    shape: _CouponShapeBorder(),
                    child: Container(
                      constraints: const BoxConstraints.expand(width: 103, height: 100),
                      child: Column(
                        children: [
                          Container(
                            padding: EdgeInsets.only(top: 15.h, bottom: 15.h),
                            alignment: Alignment.center,
                            child: Text(
                              '￥20',
                              style: StylesUtils.customTextStyle(
                                  fontWeight: FontWeight.w700, fontSize: 28.sp),
                            ),
                          ),
                          Container(
                            alignment: Alignment.center,
                            child: Text(
                              '满200元可用',
                              style: StylesUtils.customTextStyle(
                                  fontSize: StylesUtils.fontSize13, color: ColorsUtils.colorF2f2),
                            ),
                          )
                        ],
                      ),
                    ),
                  ),
                  Expanded(
                      child: Container(
                    padding: EdgeInsets.only(left: 10.w),
                    decoration: BoxDecoration(
                        color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorFfff)),
                    child: Stack(
                      children: [
                        Column(
                          children: [
                            Container(
                              padding: EdgeInsets.only(top: 15.h, bottom: 25.h),
                              alignment: Alignment.centerLeft,
                              child: Text(
                                '仅限购买服饰类部分商品',
                                style: StylesUtils.customTextStyle(color: ColorsUtils.color6666),
                              ),
                            ),
                            Container(
                              alignment: Alignment.centerLeft,
                              child: Text(
                                '2018-11-03至2018-11-20',
                                style: StylesUtils.customTextStyle(
                                    fontSize: StylesUtils.fontSize13, color: ColorsUtils.color9999),
                              ),
                            )
                          ],
                        ),
                      ],
                    ),
                  ))
                ],
              ),
            );
          },
          itemCount: 10,
        )
      ],
    );
  }
}

class _CouponShapeBorder extends ShapeBorder {
  @override
  // TODO: implement dimensions
  EdgeInsetsGeometry get dimensions => throw UnimplementedError();

  @override
  Path getInnerPath(Rect rect, {TextDirection? textDirection}) {
    // TODO: implement getInnerPath
    throw UnimplementedError();
  }

  @override
  Path getOuterPath(Rect rect, {TextDirection? textDirection}) {
    var path = Path();
    path.addRect(rect);
    // path.addArc(Rect.fromCircle(center: const Offset(0, 15), radius: 5), pi * 3 / 2, pi);
    _formHoldLeft(rect, path);
    path.fillType = PathFillType.evenOdd;
    return path;
  }

  void _formHoldLeft(Rect rect, Path path) {
    var len = 15.0;
    while (len < rect.height) {
      path.addArc(Rect.fromCircle(center: Offset(0, len), radius: 5), pi * 3 / 2, pi);
      len += 15.0;
    }
  }

  @override
  void paint(Canvas canvas, Rect rect, {TextDirection? textDirection}) {
    // TODO: implement paint
  }

  @override
  ShapeBorder scale(double t) {
    // TODO: implement scale
    throw UnimplementedError();
  }
}

class _MyCustomClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    Path path = Path();
    path.moveTo(0, 0);
    path.lineTo(30, 0);
    path.lineTo(size.width, size.height - 30);
    path.lineTo(size.width, size.height);
    path.close();
    return path;
  }

  @override
  bool shouldReclip(covariant CustomClipper<Path> oldClipper) {
    return this != oldClipper;
  }
}
